package com.mytest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class MovieController {


    @Autowired
    private RestTemplate template;
    @Autowired
    private LoadBalancerClient loadBalancerClient;
    @Autowired
    private MovieServiceClient movieServiceClient;


    @GetMapping("/movie/{id:\\d+}")
    public Movie findById(@PathVariable Integer id) {

        return movieServiceClient.findById(id);
    }

    /*@GetMapping("/movie/{id:\\d+}")
    public Movie findById(@PathVariable Integer id) {

        ServiceInstance serviceInstance = loadBalancerClient.choose("MOVIE-SERVICE-PROVIDER");
        String url = "http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"/movie/"+id;
        System.err.println(url);
        System.err.println(serviceInstance.getHost());
        System.err.println(serviceInstance.getPort());
        //Movie movie = template.getForObject(url,Movie.class);
        //Movie movie = template.getForObject("http://127.0.0.1:9090/movie/"+id,Movie.class);
        Movie movie = new Movie(1001,"无间道II","Andy Liu");
        return movie;
    }*/

    /*@GetMapping("/movie/{id:\\d+}")
    public String findById(@PathVariable Integer id) {

        String url = "http://127.0.0.1:9090/movie/"+id;
        String str = template.getForObject(url,String.class);
        System.out.println(str);
        return str;
    }*/

    /*@GetMapping("/movie/{id:\\d+}")
    public Movie findById(@PathVariable Integer id) {

        String url = "http://127.0.0.1:9090/movie/"+id;
        return template.getForObject(url,Movie.class);
    }*/
}
